package com.delivery.www.servlet;

import com.delivery.www.po.Comment;
import com.delivery.www.po.Message;
import com.delivery.www.po.Seller;
import com.delivery.www.service.ServiceImpl.CommentServiceImpl;
import com.delivery.www.service.ServiceImpl.MessageServiceImpl;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

import static com.delivery.www.servlet.SellerStorePage.storePage;

public class SellerCommentPage {
    public static void commentPage(int storeId, Seller seller) {
        CommentServiceImpl commentService = new CommentServiceImpl();
        Scanner scanner = new Scanner(System.in);
        MessageServiceImpl messageService = new MessageServiceImpl();
        List<Message> messages = messageService.getMessage(storeId);
        System.out.println("本店铺的留言如下：");
        for (Message message : messages) {
            System.out.println("====================");
            System.out.println("留言id：" + message.getMessageId());
            System.out.println("留言内容：" + message.getContent());
            System.out.println("====================");
        }
        List<Comment> comments = commentService.getComment(storeId);
        for (Comment comment : comments) {
            System.out.println("====================");
            System.out.println("评论id：" + comment.getCommentId());
            System.out.println("评论内容：" + comment.getContent());
            System.out.println("评论人：" + comment.getUserName());
            System.out.println("====================");
        }
        System.out.println("请输入你要进行的操作:");
        System.out.println("1.写留言");
        System.out.println("2.删留言");
        System.out.println("3.删评论");
        System.out.println("4.返回");
        int i = scanner.nextInt();
        while (i != 1 && i != 2 && i != 3 && i != 4) {
            System.out.println("输入错误！请重新输入");
            i = scanner.nextInt();
        }
        if (i == 1) {
            System.out.println("请输入你要写的留言内容：");
            String s = scanner.next();
            boolean b = messageService.doMessage(storeId, s);
            if (b) {
                System.out.println("留言成功！即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                commentPage(storeId, seller);
            } else {
                System.out.println("留言失败！即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                commentPage(storeId, seller);
            }
        }
        if (i == 2) {
            System.out.println("请输入你要删除留言id：");
            int i1 = scanner.nextInt();
            boolean b = messageService.deleteMessage(i1);
            if (b) {
                System.out.println("删除成功！即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                commentPage(storeId, seller);
            } else {
                System.out.println("删除失败！即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                commentPage(storeId, seller);
            }
        }
        if (i == 3) {
            System.out.println("请输入所要删除的评论id：");
            int i1 = scanner.nextInt();
            boolean b = commentService.deleteComment(i1, storeId);
            if (b) {
                System.out.println("删除成功！即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                commentPage(storeId, seller);
            } else {
                System.out.println("删除失败！即将跳转");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                commentPage(storeId, seller);
            }
        }
        if (i == 4) {
            storePage(seller);
        }
    }
}
